#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;

int power(int a, int b, int mod)
{
    long long res = 1;
    while (b--)
        res = res * a % mod;
    return res;
}

vector<int> getGoodIndices(vector<vector<int>>& vv, int target) {
    vector<int> res;
    for (int i = 0; i < vv.size(); i++)
    {
        int a = vv[i][0], b = vv[i][1], c = vv[i][2], m = vv[i][3];
        if (power(power(a, b, 10), c, m) == target)
        {
            res.push_back(i);
        }
    }
    return res;
}

//int main()
//{
//    vector<vector<int>> vv = { { 39,3,1000,1000 } };
//    getGoodIndices(vv, 17);
//    return 0;
//}

long long countSubarrays(vector<int>& nums, int k) {
    int mx = *max_element(nums.begin(), nums.end());
    long long res = 0;
    int cnt = 0, left = 0;
    for (auto x : nums)
    {
        cnt += x == mx;
        while (cnt == k)
            cnt -= nums[left++] == mx;
        res += left;
    }
    return res;
}

int main()
{
    vector<int> v = { 61,23,38,23,56,40,82,56,82,82,82,70,8,69,8,7,19,14,58,42,82,10,82,78,15,82 };
    cout << countSubarrays(v, 2);
    return 0;
}